<?PHP
if (!session_start()) { session_start(); };
/*if (!isset($_SESSION['lgnapproved'])) {
	die(header('Location: login.php'));
}*/

include("functions.php");
include("cnnxn.php");
connect();

if (isset($_POST['submit'])) {
	include("simpleresize.php");
	
	$fieldnames="";
	$fieldvalues="";
	while (list($key,$value) = each($_POST)) {
	if ($key!='submit' && $key!='PHPSESSID') {
		$fieldnames=$fieldnames.$key.',';
		if (ctype_digit($value)) {
		   $fieldvalues=$fieldvalues.$value.',';
	   } else {
		   $fieldvalues=$fieldvalues.'"'.addslashes($value).'",';
	   }
	}
	}
	
	if (isset($_FILES['Picture']) && $_FILES['Picture']['name']!='') {
		$image = new SimpleImage();
		$image->load($_FILES['Picture']['tmp_name']);	
		list($width, $height) = getimagesize($_FILES['Picture']['tmp_name']);
		$setWidth=200;
		$setHeight=200;
		if ($width>=$height) {
			if ($setWidth<=$width) {
				$image->resizeToWidth($setWidth);
			}
		} else {
			if ($setHeight<=$height) {
				$image->resizeToHeight($setHeight);
			}
		}
		$image->save('../images/players/'.$_FILES['Picture']['name']);
		$fieldnames=$fieldnames.'Picture,';
		$fieldvalues=$fieldvalues.'"'.$_FILES['Picture']['name'].'",';
	}
	$fieldnames=substr_replace($fieldnames,"",-1);
	$fieldvalues=substr_replace($fieldvalues,"",-1);
	$SQLadd='INSERT INTO Players ('.$fieldnames.') VALUES ('.$fieldvalues.');';
	//print $SQLadd;
	$sendit_add=mysqli_query($db,$SQLadd) or die(mysqli_error($db));
	
	mysqli_close($db);
	
	die(header("Location: Players_viewall.php"));
}
?>